﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//using AuthorizeNet;
using System.Configuration;
using AuthorizeNet;


public partial class Controls_c_WebUserControl : System.Web.UI.UserControl
{

    //------------------------------------------------------------------------------------------------------------------------------------------+
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckOutHeader.Text = "Your Shopping Cart is Empty";
        LabelCartHeader.Text = "";
        //-------------------------------------------------------
        //TEST DATA
        CreditCardNumberTxtBox.Text = "4111111111111111";
        CCVTxtBox.Text = "0116";
        ExpDateTxtBox.Text = "123";
    }
    //-------ADDED PAYMENT METHODS----- SALE
    private IGateway OpenGateway()
    {
        var login = ConfigurationManager.AppSettings["ApiLogin"];
        var transactionKey = ConfigurationManager.AppSettings["TransactionKey"];

        var gate = new Gateway(login, transactionKey, true);
        return gate;
    }

    private string ExecutePayment()
    {
        var gate = OpenGateway();
        var apiRequest = CheckoutFormReaders.BuildAuthAndCaptureFromPost();

        try
        {
            String price = "100";
            String cardNumber = CreditCardNumberTxtBox.Text;
            String expDate = ExpDateTxtBox.Text;
            String ccv = CCVTxtBox.Text;

            apiRequest.Queue(ApiFields.Amount, price);
            apiRequest.Queue(ApiFields.CreditCardNumber, cardNumber);
            apiRequest.Queue(ApiFields.CreditCardExpiration, expDate);
            apiRequest.Queue(ApiFields.CreditCardCode, ccv);
        }
        catch
        {
            return "error";
        }

        var response = gate.Send(apiRequest);

        if (response.Approved)
        {
            //CommerceEntities db = new CommerceEntities();
            string AuthCode = response.AuthorizationCode;
            string TransactionID = response.TransactionID;  //// da se sachuva

            string OrderMessage = string.Format("Thank you! Order approved: {0}", response.AuthorizationCode);

            MsgLbl.Text = "Response message is: " + response.Message + "</br>" + "Response code is: " + response.ResponseCode;

            return TransactionID;
        }
        else
        {
            string OrderMessage = response.Message;
            MsgLbl.Text = "Response message is: " + response.Message + "</br>" + "Response code is: " + response.ResponseCode;
            return "error";
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //------------ADDED MY CODE
        string transactionID = ExecutePayment();
        if (transactionID != "error")
        {
            CheckOutHeader.Text = "Transaction succesful. ";
        }
        else
        {
            CheckOutHeader.Text = "Transaction Failed. ";
        }

    }
}

